//
//  Q&AInfoViewController.m
//  xpkc
//
//  Created by TopSageOSX on 15/2/28.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "Q&AInfoViewController.h"
#import "QAViewCell.h"



@interface Q_AInfoViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation Q_AInfoViewController
{
    UITableView * qatableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"答疑详情";
//    [self creatInfoView];
    [self createTableview];
    NSLog(@"%@",self.replyList);
}

-(void)createTableview
{
    self.view.backgroundColor = BGColor;
    qatableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-60)];
    qatableView.delegate = self;
    qatableView.dataSource = self;
    qatableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    qatableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:qatableView];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (self.replyList.count==0) {
//        return 1;
//    }else{
//        return self.replyList.count + 1;
//    }
    return self.replyList.count + 1;
    // return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CGFloat offsize = 0.04*SCREEN_WIDTH;
    
    if (indexPath.row == 0) {
        
        
        NSMutableString * mutStr = [NSMutableString stringWithString:self.askModel.myaskcontent];
        [mutStr replaceOccurrencesOfString:@"<p>" withString:@"\n\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, self.askModel.myaskcontent.length - 1)];
        
        [mutStr replaceOccurrencesOfString:@"</p>" withString:@"\n" options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, mutStr.length - 1)];
        
        [mutStr replaceOccurrencesOfString:@"<br/>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, mutStr.length - 1)];
        CGSize contentSize = [mutStr sizeWithFont:FONT_SYS_M constrainedToSize:CGSizeMake(SCREEN_WIDTH-offsize - offsize * 2 , 2000)];
        
        if (contentSize.height > 55) {
            
            return 120 + contentSize.height - 55 + 30;
            
        }else{
            
            return 120;
        }
        
    }else{
        
        /*
        “—” 代表切换界面的意思      问题：A—B,在B里面得到数据11后，切换传值用属性str1返回到A保存数据11，后来又A—B，在B里面得到数据22后,切换传值用属性str2返回到A保存数据22。 这样，原来的数据11丢失了。  怎么办
        */
        
        
        
//        NSLog(@"%ld--%@",indexPath.row,self.replyList);
        
        NSDictionary * dic = self.replyList[indexPath.row - 1];
        
//        NSLog(@"%@",dic);
        
        NSMutableString * mutStr = [[NSMutableString alloc] initWithString:[dic objectForKey:@"content"]];
        
        [mutStr replaceOccurrencesOfString:@"<p>" withString:@"\n\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [[dic objectForKey:@"content"] length])];
        
        [mutStr replaceOccurrencesOfString:@"</p>" withString:@"\n" options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, mutStr.length)];
        CGSize contentSize = [mutStr sizeWithFont:FONT_SYS_M constrainedToSize:CGSizeMake(SCREEN_WIDTH-offsize - offsize * 2 , 2000)];
        
        if (contentSize.height > 55) {
            
            return 120 + contentSize.height - 55 + 30;
            
        }else{
            
            return 120;
        }
        
    }
    
}
- (QAViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QAViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[QAViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
     cell.text.hidden=YES;
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSLog(@"%@",self.replyList);

    if (indexPath.row==0) {
//        cell.questionStr = self.myaskcontent;
//        cell.timeStr =self.asktime;
//        cell.myname.text=self.myname;
//        cell.myasktitle.text=self.myasktitle;
        
        [cell configWithAskModel:self.askModel];
        
    }else {
        
        //for (NSDictionary * dic in self.replyList) {
        NSDictionary * dic = self.replyList[indexPath.row-1];
        
        NSLog(@"row--%ld",indexPath.row);
        NSLog(@"dic--%@",dic);
        NSLog(@"%@",[dic objectForKey:@"content"]);
        
            NSMutableString * mutStr = [[NSMutableString alloc] initWithString:[dic objectForKey:@"content"]];
            
            [mutStr replaceOccurrencesOfString:@"<p>" withString:@"\n\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [[dic objectForKey:@"content"] length])];
            
            [mutStr replaceOccurrencesOfString:@"</p>" withString:@"\n" options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, mutStr.length)];
            myAskModel * askModel = [[myAskModel alloc] init];
            
            askModel.myaskcontent = mutStr;
            askModel.asktime = [dic objectForKey:@"replytime"];
            askModel.myname = [dic objectForKey:@"username"];
            askModel.myasktitle = @"";
            /*
             ==刚才发得里面陈述错了，不看那个，看这个==============“—” 代表切换界面的意思      问题：A—B,在B里面得到数据11后，切换传值用属性str1返回到A保存数据11，后来又A—C，在C里面得到数据22后,切换传值用属性str2返回到A保存数据22。 这样，原来的数据11丢失了。  怎么办
             
             你就不能一次传完么
             */
            [cell configWithAskModel:askModel];
        
        //    cell.questionStr = model.myaskcontent;
        //    cell.timeStr =model.asktime;
        //    cell.myname.text=model.myname;
        //    cell.myasktitle.text=model.myasktitle;
        //    if (model.ReplyList.count) {
        //        cell.msgState=MsgStateRecive;
        //    }else{
        //    cell.msgState = MsgStateWait;
        //    }
        
//            cell.questionStr=mutStr;
//            cell.timeStr=[dic objectForKey:@"replytime"];
//            cell.myname.text=[dic objectForKey:@"username"];

        //}
        

    }
   
    return cell;
    

}

//- (void)creatInfoView{
//    
//    UILabel * teaLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 50)];
//    teaLabel.text=self.username;
//    teaLabel.font=[UIFont boldSystemFontOfSize:20];
//    teaLabel.textColor=[UIColor redColor];
//    [self.view addSubview:teaLabel];
//    
//    UILabel * timeLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 50, SCREEN_WIDTH, 10)];
//    timeLabel.text=self.replytime;
//    timeLabel.font=[UIFont systemFontOfSize:12];
//    timeLabel.textColor=[UIColor blackColor];
//    [self.view addSubview:timeLabel];
//    
//    UILabel * context=[[UILabel alloc] initWithFrame:CGRectMake(20, 60, SCREEN_WIDTH-40, 100)];
//    
//    //    NSLog(@"%@",self.content);
//    
//    NSMutableString * mutStr = [[NSMutableString alloc] initWithString:self.content];
//    
//    [mutStr replaceOccurrencesOfString:@"<p>" withString:@"\n\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, self.content.length)];
//    
//    [mutStr replaceOccurrencesOfString:@"</p>" withString:@"\n" options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, mutStr.length)];
//    
//    context.text=mutStr;
//    context.font=[UIFont boldSystemFontOfSize:15];
//    context.textColor=[UIColor blackColor];
//    context.numberOfLines=0;
//    [self.view addSubview:context];
//    
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
